home *** CD-ROM | disk | FTP | other *** search
/ CD Exchange / CD Exchange - Volume 1.iso / graphics / utils / ham8-jpeg / source / al-j.c < prev    next >
C/C++ Source or Header  |  1992-12-20  |  13KB  |  570 lines

  1. /*
  2.  * Al-j.c Michael Saunby    M.Saunby@reading.ac.uk
  3.  *
  4.  * Release 1.1  December 1992 Jpeg viewer using Albert HAM8 public screen and
  5.  * Independent JPEG groups jpeg decompressor.
  6.  */
  7. #define PROG_NAME "Al-J JPEG Viewer"
  8. #define COPYRIGHT_MESSAGE "Version 1.1\nDecember 1992\n\nCopyright © 1992\n\
  9. Michael Saunby\n\
  10. \nThis program is based in part\n\
  11. on the work of the Independent\n\
  12. JPEG Group"
  13.  
  14. #include <intuition/intuition.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <graphics/gfxbase.h>
  17. #include <graphics/displayinfo.h>
  18. #include <intuition/screens.h>
  19. #include <dos/dosextens.h>
  20. #include <dos/dosasl.h>
  21. #include <libraries/gadtools.h>
  22.  
  23. #include <clib/asl_protos.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/intuition_protos.h>
  26. #include <clib/layers_protos.h>
  27. #include <clib/graphics_protos.h>
  28. #include <clib/gadtools_protos.h>
  29. #include <clib/dos_protos.h>
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include "display24.h"
  36.  
  37. /* Values greater than 1 give bad pixels, why? */
  38. #define ARRAY24_MAX_ROWS 1
  39.  
  40. /*
  41.  * ReadArgs() template
  42.  */
  43. #define TEMPLATE "NAME"
  44. #define OPT_NAME 0
  45. #define OPT_COUNT 1
  46.  
  47. int JPEG_restart = 0;        /* set to 1 for New file */
  48.  
  49. struct IntuitionBase *IntuitionBase = NULL;
  50. struct GfxBase *GfxBase = NULL;
  51. struct Library *GadToolsBase = NULL;
  52. struct Library *AslBase = NULL;
  53. extern struct ExecBase *SysBase;
  54.  
  55. /* following for 24 bit put row */
  56. SHORT ham8_line;        /* screen line number to draw at */
  57. UBYTE *ham8_pens = NULL;
  58. struct RastPort ham8_temprp;
  59. struct BitMap *temp_bm = NULL;
  60. struct Screen *screen = NULL;
  61. struct FileRequester *filereq = NULL;
  62. struct BitMap *bitmap = NULL;
  63. struct Window *window = NULL;
  64. struct Menu *menu;
  65. struct Gadget *gadgets, *horprop = NULL, *vertprop = NULL;
  66. void *vi;
  67. ULONG allocsignal;
  68.  
  69. /* gadget ids */
  70. #define HORPROP     1
  71. #define VERTPROP     2
  72. /* current x and y offsets */
  73. LONG scroll_x = 0;
  74. LONG scroll_y = 0;
  75. LONG window_max_w;
  76. LONG window_max_h;
  77.  
  78. struct EasyStruct failedES =
  79. {sizeof (struct EasyStruct), 0, PROG_NAME,
  80.  "%s", "OK",};
  81.  
  82. #define OPEN 1
  83. #define ABOUT 2
  84. #define QUIT 3
  85. struct NewMenu prj_menu[] =
  86. {
  87.   {NM_TITLE, "Project", 0, 0, 0, 0,},
  88.   {NM_ITEM, "Open...", "O", 0, 0, (void *) OPEN,},
  89.   {NM_ITEM, NM_BARLABEL, 0, 0, 0, 0,},
  90.   {NM_ITEM, "About...", "?", 0, 0, (void *) ABOUT,},
  91.   {NM_ITEM, "Quit", "Q", 0, 0, (void *) QUIT,},
  92.   {NM_END, NULL, 0, 0, 0, 0,},
  93. };
  94.  
  95. void
  96. Cleanup ()
  97. {
  98.   if (screen)
  99.     UnlockPubScreen (NULL, screen);
  100.   screen = NULL;
  101.  
  102.   if (window)
  103.     {
  104.       ReleasePens (&(window->WScreen->ViewPort));
  105.       FreeVisualInfo (vi);
  106.       CloseWindow (window);
  107.       gadgets = NULL;
  108.     }
  109.   window = NULL;
  110.  
  111.   if (menu)
  112.     ClearMenuStrip (window);
  113.   menu = NULL;
  114.   if (horprop)
  115.     DisposeObject (horprop);
  116.   horprop = NULL;
  117.   if (vertprop)
  118.     DisposeObject (vertprop);
  119.   vertprop = NULL;
  120.   if (ham8_pens)
  121.     FreeMem (ham8_pens, ((temp_bm->BytesPerRow << 3) * temp_bm->Rows));
  122.   ham8_pens = NULL;
  123.   if (temp_bm)
  124.     FreeBitMap (temp_bm);
  125.   temp_bm = NULL;
  126.   if (bitmap)
  127.     FreeBitMap (bitmap);
  128.   bitmap = NULL;
  129.   if (!JPEG_restart)
  130.     {
  131.       if (filereq)
  132.     FreeAslRequest (filereq);
  133.       filereq = NULL;
  134.       if (AslBase)
  135.     CloseLibrary (AslBase);
  136.       AslBase = NULL;
  137.       if (GadToolsBase)
  138.     CloseLibrary (GadToolsBase);
  139.       GadToolsBase = NULL;
  140.       if (IntuitionBase)
  141.     CloseLibrary ((struct Library *) IntuitionBase);
  142.       IntuitionBase = NULL;
  143.       if (GfxBase)
  144.     CloseLibrary ((struct Library *) GfxBase);
  145.       GfxBase = NULL;
  146.     }
  147. }
  148.  
  149. void
  150. Quit (char whytext[], UBYTE failcode)
  151. {
  152.   EasyRequest (NULL, &failedES, NULL, whytext);
  153.   Cleanup ();
  154.   exit (failcode);
  155. }
  156.  
  157. void
  158. UserWait ()
  159. {
  160.   struct IntuiMessage *msg;
  161.   struct Gadget *gadget;
  162.   BOOL abort = FALSE;
  163.   UWORD class, code;
  164.   LONG new_x, new_y;
  165.  
  166.   SetGadgetAttrs (horprop, window, NULL,
  167.           GA_Disabled, FALSE,
  168.           TAG_END);
  169.   SetGadgetAttrs (vertprop, window, NULL,
  170.           GA_Disabled, FALSE,
  171.           TAG_END);
  172.  
  173.   do
  174.     {
  175.       WaitPort (window->UserPort);
  176.  
  177.       while (msg = GT_GetIMsg (window->UserPort))
  178.     {
  179.       code = msg->Code;
  180.       class = msg->Class;
  181.       gadget = (struct Gadget *) msg->IAddress;
  182.       GT_ReplyIMsg (msg);
  183.       switch (class)
  184.         {
  185.         case IDCMP_MENUPICK:
  186.           switch ((UWORD) MENU_USERDATA (ItemAddress (menu, code)))
  187.         {
  188.         case QUIT:
  189.           abort = TRUE;
  190.           break;
  191.         case OPEN:
  192.           JPEG_restart = TRUE;
  193.           abort = TRUE;
  194.           break;
  195.         case ABOUT:
  196.           EasyRequest (window, &failedES,
  197.                    NULL,
  198.                    COPYRIGHT_MESSAGE);
  199.           break;
  200.         }
  201.           break;
  202.         case IDCMP_CLOSEWINDOW:
  203.           abort = TRUE;
  204.           break;
  205.         case IDCMP_SIZEVERIFY:
  206.         case IDCMP_NEWSIZE:
  207.           SetGadgetAttrs (vertprop, window, NULL,
  208.                   PGA_Visible, window->GZZHeight,
  209.                   TAG_END);
  210.           SetGadgetAttrs (horprop, window, NULL,
  211.                   PGA_Visible, window->GZZWidth,
  212.                   TAG_END);
  213.           break;
  214.         case IDCMP_GADGETUP:
  215.           switch (gadget->GadgetID)
  216.         {
  217.         case HORPROP:
  218.           GetAttr (PGA_Top, horprop, &new_x);
  219.           ScrollLayer (0L, window->RPort->Layer, new_x - scroll_x, 0);
  220.           scroll_x = new_x;
  221.           WindowLimits (window, 0, 0, window_max_w - scroll_x, 0);
  222.           break;
  223.  
  224.         case VERTPROP:
  225.           GetAttr (PGA_Top, vertprop, &new_y);
  226.           ScrollLayer (0L, window->RPort->Layer, 0, new_y - scroll_y);
  227.           scroll_y = new_y;
  228.           WindowLimits (window, 0, 0, 0, window_max_h - scroll_y);
  229.           break;
  230.         }
  231.  
  232.           break;
  233.         default:
  234.           /*
  235.            * EasyRequest(window, &failedES, NULL, "Bogus message
  236.            * sorry!");
  237.            */
  238.           break;
  239.         }
  240.  
  241.     }
  242.   } while (abort == FALSE);
  243.  
  244.   Cleanup ();
  245.  
  246. }
  247.  
  248. char *
  249. Startup (char *namebuf)
  250. {
  251.   char *ret_code = NULL;
  252.   LONG result[OPT_COUNT] =
  253.   {0};
  254.   struct RDArgs *rda;
  255.  
  256.   if (!JPEG_restart)
  257.     {
  258.       if ((GfxBase =
  259.        (struct GfxBase *) OpenLibrary ("graphics.library", 36)) == NULL)
  260.     Quit ("graphics.library is too old <V36", 25);
  261.  
  262.       if ((IntuitionBase =
  263.        (struct IntuitionBase *) OpenLibrary ("intuition.library", 36)) == NULL)
  264.     Quit ("intuition.library is too old <V36", 25);
  265.  
  266.       if ((AslBase = OpenLibrary ("asl.library", 36)) == NULL)
  267.     Quit ("asl.library is too old <V36", 25);
  268.  
  269.       if ((GadToolsBase = OpenLibrary ("gadtools.library", 36)) == NULL)
  270.     Quit ("gadtools.library is too old <V36", 25);
  271.  
  272.       if ((filereq =
  273.        (struct FileRequester *) AllocAslRequestTags (ASL_FileRequest,
  274.                       ASLFR_PubScreenName, HAM8_ALBERT_NAME,
  275.                              TAG_END))
  276.       == NULL)
  277.     Quit ("could not build file requster", 25);
  278.  
  279.       rda = ReadArgs (TEMPLATE, result, NULL);
  280.  
  281.       if (result[OPT_NAME])
  282.     {
  283.       strcpy (namebuf, (UBYTE *) result[OPT_NAME]);
  284.       ret_code = namebuf;
  285.     }
  286.       FreeArgs (rda);
  287.     }
  288.   JPEG_restart = FALSE;
  289.  
  290.   if (ret_code == NULL)
  291.     {
  292.       struct Screen *screen;
  293.       /*
  294.        * Opening the File Requester does not bring screen to front. Passing
  295.        * the name ie. UnlockPubscreen(HAM8_ALBERT_NAME, NULL) is allowed
  296.        * but not recommended. I haven't tried it.
  297.        */
  298.       if ((screen = LockPubScreen (HAM8_ALBERT_NAME)) != NULL)
  299.     {
  300.       ScreenToFront (screen);
  301.       UnlockPubScreen (NULL, screen);
  302.     }
  303.       if (AslRequest (filereq, 0L))
  304.     {
  305.       if(filereq->rf_Dir[strlen(filereq->rf_Dir)-1] == ':')
  306.       sprintf (namebuf, "%s%s", filereq->rf_Dir, filereq->rf_File);
  307.       else
  308.       sprintf (namebuf, "%s/%s", filereq->rf_Dir, filereq->rf_File);
  309.       ret_code = namebuf;
  310.     }
  311.     }
  312.   ham8_line = 0;
  313.  
  314.   return (ret_code);
  315. }
  316.  
  317.  
  318. void
  319. CreateWindow (UWORD width, UWORD height)
  320. {
  321.   int pen_errors;
  322.  
  323.   struct Gadget **tmpgad;
  324.  
  325.   if ((menu = CreateMenus (prj_menu, GTMN_FrontPen, 1, TAG_DONE)) == NULL)
  326.     Quit ("could not build menu", 25);
  327.  
  328.   if ((screen = LockPubScreen (HAM8_ALBERT_NAME)) == NULL)
  329.     Quit ("could not find screen", 25);
  330.  
  331.   if ((bitmap = AllocBitMap (width, height, 8, BMF_DISPLAYABLE | BMF_CLEAR,
  332.                  NULL)) == NULL)
  333.     Quit ("could not allocate bitmap", 25);
  334.   gadgets = NULL;
  335.   tmpgad = &gadgets;
  336.  
  337.   /* Create scroller gadgets */
  338.   horprop = (struct Gadget *) NewObject (NULL, "propgclass",
  339.                      GA_Immediate, TRUE,
  340.                      GA_BottomBorder, TRUE,
  341.                      GA_GZZGadget, TRUE,
  342.                      GA_RelVerify, TRUE,
  343.                      PGA_Freedom, FREEHORIZ,
  344.                      PGA_Borderless, FALSE,
  345.                      PGA_NewLook, TRUE,
  346.                      GA_RelBottom, -10,
  347.                      GA_RelWidth, -21,
  348.                      GA_Left, 3,
  349.                      GA_Height, 7,
  350.                      PGA_